package p2plending.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import p2plending.domain.CuentaCorriente;
import p2plending.utils.UIDGenerator;

public class JDBCCuentaCorrienteDAO implements ICuentaCorrienteDAO{
	
	private Connection conn;
	private IUsuarioDAO iu;
	
	public JDBCCuentaCorrienteDAO(){
		conn = ConnectionManager.getInstance().checkOut();
		System.out.println("Se ha creado la conexion correctamente");
		iu= new JDBCUsuarioDAO();
	}
	
	public void delete(String numCuenta){
		String sql = "DELETE FROM cuentacorriente WHERE (numCuenta = ?) ";
	    PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, numCuenta);  
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
       } finally {
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }
        }
	}

	public void insert(String numCuenta, String titular, String nombreUsuario) {
		PreparedStatement stmt = null;
        String sql = "INSERT INTO cuentacorriente (ccOID, usuarioOID, numCuenta, titular) VALUES (?, ?, ?, ?) ";

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, UIDGenerator.getInstance().getKey());
            stmt.setString(2, iu.selectOIDUsuario(nombreUsuario));
            stmt.setString(3, numCuenta);
            stmt.setString(4, titular);
   
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }	
	}
	
	public CuentaCorriente select(String numCuenta){
		String sql = "SELECT * FROM cuentacorriente WHERE (numCuenta = ? ) ";
        PreparedStatement stmt = null;
        ResultSet result = null;
        CuentaCorriente c= null;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, numCuenta);
            result = stmt.executeQuery();

            result.next();
            c = new CuentaCorriente();
            c.setNumCuenta(result.getString("numCuenta"));
            c.setTitular(result.getString("titular"));
    
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return c;
    }	
}